package com.happyghost.reflect;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class RefUserMainTest {


    /**
     * 比较2个User对象属性不同
     * @param oldUser
     * @param newUser
     * @return
     */
    public static Map<String, String> diffUser(RefUser oldUser, RefUser newUser) {
        Map<String, String> diffMap = new HashMap<>();

        if ((oldUser.getId() == null && newUser.getId() != null) ||
                (oldUser.getId() != null && !oldUser.getId().equals(newUser.getId()))) {
            diffMap.put("id", "from " + oldUser.getId() + " to " + newUser.getId());
        }

        if ((oldUser.getName() == null && newUser.getName() != null) ||
                (oldUser.getName() != null && !oldUser.getName().equals(newUser.getName()))) {
            diffMap.put("name", "from " + oldUser.getName() + " to " + newUser.getName());
        }

        return diffMap;
    }


    /**
     * 比较两个任意对象的属性不同
     * @param oldObj
     * @param newObj
     * @return
     */
    public static Map<String, String> diffObject(Object oldObj, Object newObj) {
        Map<String, String> diffMap = new HashMap<>();
        try {
            // 获取对象的类
            Class oldObjClazz = oldObj.getClass();
            Class newObjClazz = newObj.getClass();

            // 判断两个对象是否属于同一个类
            if (oldObjClazz.equals(newObjClazz)) {
                // 获取对象的所有属性
                Field[] fields = oldObjClazz.getDeclaredFields();
                // 对每个属性逐一判断
                for (Field field : fields) {
                    // 使属性可以被反射访问
                    field.setAccessible(true);
                    // 获取当前属性的值
                    Object oldValue = field.get(oldObj);
                    Object newValue = field.get(newObj);
                    // 如果某个属性的值在两个对象中不同，则进行记录
                    if ((oldValue == null && newValue != null) || oldValue != null && !oldValue.equals(newValue)) {
                        diffMap.put(field.getName(), " from " + oldValue + " to " + newValue);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return diffMap;
    }

    public static void main(String[] args) {
        RefUser oldUser = new RefUser(1, "jack");
        RefUser newUser = new RefUser(1, "jacker");
        System.out.println("不用反射，只能比较单一类型的对象：");

        Map<String, String> diffUserMap = diffUser(oldUser, newUser);
        for (Map.Entry<String, String> entry : diffUserMap.entrySet()) {
            System.out.println("属性" + entry.getKey() + "; 变化为：" + entry.getValue());
        }

        System.out.println("使用反射，可以比较属性不同的各类对象：");

        Map<String, String> diffObjMap = diffObject(oldUser, newUser);
        for (Map.Entry<String, String> entry : diffObjMap.entrySet()) {
            System.out.println("属性" + entry.getKey() + "; 变化为：" + entry.getValue());
        }

        Book oldBook = new Book("java oop", 25.1);
        Book newBook = new Book("java oop", 13.5);
        diffObjMap = diffObject(oldBook, newBook);

        for (Map.Entry<String, String> entry : diffObjMap.entrySet()) {
            System.out.println("属性" + entry.getKey() + "; 变化为：" + entry.getValue());
        }
    }


}
